package Ep06_BinaryTree.BFS.T5_0116;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/4/20 15:47
 * 说明：
 */

public class Mine {
    public Node connect(Node root) {
        if (root == null) return null;
        Deque<Node> deque = new LinkedList<>();
        deque.offer(root);
        while(!deque.isEmpty()){
            int size = deque.size();
            Node past = null;
            while (size > 0){
                Node cur = deque.poll();
                if (cur.left != null){
                    deque.offer(cur.left);
                }
                if (cur.right != null){
                    deque.offer(cur.right);
                }
                if (past != null) past.next = cur;
                past = cur;
                size--;
            }
        }
        return root;
    }
}

class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
